package com.huawei.hgip.paintgame.util;

import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.huawei.hgip.paintgame.activity.MainActivity;
import com.huawei.hgip.paintgame.m.data.UserProfile;

public class RegistLoginThread extends Thread {
	
	private MainActivity mainActivity;
	public  UserProfile userProfile = new UserProfile();
	//手机型号
	private String MODEL = null; 
	private String IMEI = null;
	TelephonyManager tm;
	
	public RegistLoginThread(MainActivity mainActivity,TelephonyManager telephonyManager){
		this.mainActivity = mainActivity;
		tm = telephonyManager;
	}
	
    @Override
    public void run() {
        Message message0=new Message();
        Message message1=new Message();
        Message message2=new Message();
        Message message3=new Message();
        Message message11=new Message();
        Message message12=new Message();
        Message message13=new Message();
        Message message14=new Message();
        
        //MessageQueue messageQueue;
        
        System.out.println("RegistLoginThread ID:"+Thread.currentThread().getId()+"!\n");
    	SharedPreferences userIdPreferences = mainActivity.getSharedPreferences("userId", Context.MODE_PRIVATE);
    	Editor userEditor = userIdPreferences.edit();
        int userId = userIdPreferences.getInt("userId", 0);      
        System.out.println("userIdPreferences userId001:"+userId+"!\n");   
        //用户登录 userId 初始值为0 ，如果为0则需要注册，如果不为0 ，则直接登录。
        	if (0==userId) {
        		//如果登录失败先注册
        		message0.what = 0;
        		_handlerRegistLogin.sendMessage(message0);
        		//Toast.makeText(mainActivity, "首次登录", Toast.LENGTH_SHORT).show();        		
                System.out.println("TestStep 001:\n"+"first login"+"\n 001 end.");
        		MODEL = android.os.Build.MODEL;
        		//手机IMEI 权限：在manifest.xml文件中要添加 <uses-permission android:name="android.permission.READ_PHONE_STATE" />
            	//获取当前手机信息必须使用到 context中的全局变量TELEPHONY_SERVICE
            	IMEI = tm.getDeviceId();
            	System.out.println("TestStep 001xx:\n"+IMEI+"\n 001xx end.");
        		try {
					userProfile.userRegister(IMEI ,MODEL);
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					//mainActivity.popupException(PGUtil.EXCE_NET_OR_SERVER);
					message11.what = 11;
	        		_handlerRegistLogin.sendMessage(message11);
					e.printStackTrace();
				} catch (SocketException e) {
					// TODO Auto-generated catch block
					//mainActivity.popupException(PGUtil.EXCE_NET_OR_SERVER);
					//首次登录不成功很有可能APN设置为wap的问题
					message14.what = 14;
	        		_handlerRegistLogin.sendMessage(message14);
					e.printStackTrace();
				} catch (IOException e) {
					message12.what = 12;
	        		_handlerRegistLogin.sendMessage(message12);
					//mainActivity.popupException(PGUtil.EXCE_APN_WAP);
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		userId=UserProfile.userId ;
        		System.out.println("userIdPreferences userId002:"+userId+"!\n");
        		if (0==userId) {
        			message3.what = 3;
	        		_handlerRegistLogin.sendMessage(message3);
        			//Toast.makeText(mainActivity, "注册失败", Toast.LENGTH_SHORT).show();
				} else {
					userEditor.putInt("userId", userId);
					userEditor.commit();
					System.out.println("userIdPreferences userId003:"+userIdPreferences.getInt("userId", 0)+"!\n");
					message2.what = 2;
	        		_handlerRegistLogin.sendMessage(message2);
					//Toast.makeText(mainActivity, "注册成功", Toast.LENGTH_SHORT).show();
				}
//        		//注册成功后再登录
//        		try {
//					userProfile.userLogin();
//				} catch (UnknownHostException e) {
//					message11.what = 11;
//	        		_handlerRegistLogin.sendMessage(message11);
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (SocketException e) {
//					message11.what = 11;
//	        		_handlerRegistLogin.sendMessage(message11);
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				} catch (IOException e) {
//					message12.what = 12;
//	        		_handlerRegistLogin.sendMessage(message12);
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				message1.what = 1;
//        		_handlerRegistLogin.sendMessage(message1);
//        		//Toast.makeText(mainActivity, "登录成功", Toast.LENGTH_SHORT).show();
			} else {
				UserProfile.userId =userId;
				try {
					userProfile.userLogin();
					message1.what = 1;
	        		_handlerRegistLogin.sendMessage(message1);
				} catch (UnknownHostException e) {
					message11.what = 11;
	        		_handlerRegistLogin.sendMessage(message11);
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SocketException e) {
					message11.what = 11;
	        		_handlerRegistLogin.sendMessage(message11);
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					message12.what = 12;
	        		_handlerRegistLogin.sendMessage(message12);
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
    }
	public Handler _handlerRegistLogin = new Handler() {
	    public void handleMessage(Message msg) {
	        switch (msg.what) {
	        case 0:
	        	Toast.makeText(mainActivity, "首次登录", Toast.LENGTH_SHORT).show();  
	        	break;
	        case 1:
	        	Toast.makeText(mainActivity, "登录成功", Toast.LENGTH_SHORT).show();
	        	break;
	        case 2:
	        	Toast.makeText(mainActivity, "注册成功", Toast.LENGTH_SHORT).show();
	        	break;
	        case 3:
	        	Toast.makeText(mainActivity, "注册失败", Toast.LENGTH_SHORT).show();
	        	break;
	        case 11:
	        	mainActivity.popupException(PGUtil.EXCE_NET_OR_SERVER);
	        	break;
	        case 12:
	        	mainActivity.popupException(PGUtil.EXCE_SDCARD);
	        	break;
	        case 13:
	        	mainActivity.popupException(PGUtil.EXCE_UNKNOW);
	        	break;
	        case 14:
	        	mainActivity.popupException(PGUtil.EXCE_APN_WAP);
	        	break;
	        }
	    }
	};
}
